import {
  Component, Input,
  OnInit,
} from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { FormService } from '@core/services/form.service';
import { deepClone } from '../../../../../utils/utils';
import { ApesDrawerCustomGridComponent } from '@shared/ag-grid-ext/drawer-custom/drawer-custom-grid.component';
import { DrawerVehicleMasterControlComponent } from '@shared/drawer-vehicleMasterControl';
import { ApesDrawerService } from '@shared/apes/drawer';
import { ExpressionService } from '@core/services/expression/ExpressionService';

@Component({
  selector: 'apes-data-driven',
  templateUrl: './dataDriven.component.html',
  styleUrls: ['./dataDriven.component.scss'],
})
export class DataDrivenComponent implements OnInit {

  // @Input() department: any; // ECC001
  @Input() config: any;
  @Input() init: boolean = true;

  @Input()
  set searchData(value: boolean) {
    if (value) {
      this.getDataList();
    }
    this.search = value;
  }

  get searchData() {
    return this.search;
  }

  isLoading: boolean = false;
  search: boolean = false;

  dataList: any[] = [
    // {
    //   title: '价格预警统计表',
    //   span: '12',
    //   list: [
    //     {
    //       LEFTTEXT: '待提交方案',
    //       RIGHTTEXT: '今天/昨天（100/200）',
    //     },
    //   ],
    // }
  ];

  constructor(
    private formService: FormService,
    private apiService: ApiService,
    protected drawerService: ApesDrawerService,
    private expressionService: ExpressionService,
  ) {
  }

  ngOnInit() {
    if (this.init) {
      this.getDataList();
    }
  }

  getDataList() {
    this.dataList = deepClone(this.config);

    this.dataList.forEach((list) => {
      let api = list.api;
      this.isLoading = true;
      let setData = list.resData ? list.resData : { body: null };
      this.apiService.post(api, setData)
        .subscribe((body) => {

          if (body) {
            list.list = body;
          }

          this.isLoading = false;
        });
    });


  }

  checkList(list, item) {
    let config = list.drawer;
    if (!config.hasOwnProperty('formId')) return;
    let width = config.width ? config.width : '38vw';
    let drawer;

    if (config.hasOwnProperty('width')) {
      width = config.width;
    }

    const openDrawer = drawer ? drawer : ApesDrawerCustomGridComponent;

    const data = {
      ...item,
    };

    let formId = this.expressionService.evaluate(config.formId, {
      data: item,
    });
    if(formId == null) {
      formId = config.formId;
    }

    let title = this.expressionService.evaluate(config.title, {
      data: item,
    });
    if(title == null) {
      title = config.title;
    }

    const drawerRef = this.drawerService.create<ApesDrawerCustomGridComponent, { formId: string, api: string, data: any }, string>({
      apesTitle: title,
      apesContent: openDrawer,
      apesWidth: width,
      apesContentParams: {
        formId: formId,
        api: config.api ? config.api : '',
        data: data,
      },
    });
    drawerRef.afterOpen.subscribe(() => {

    });

    drawerRef.afterClose.subscribe(data => {

    });
  }

}
